﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    public enum ScreenState
    {
        Active,
        Pause,
        Exit
    }

    public class ScreenManager
    {
        #region Declare
        
        public ScreenState screenState = ScreenState.Active;
        //실제 화면에 출력될 GameScreen 객체가 추가 되는 List
        List<GameScreen> screens = new List<GameScreen>();
        //screens에 들어있는 GameScreen 객체의 Update를 위해 사용되는 List
        List<GameScreen> screensToUpdate = new List<GameScreen>();
        //실제 화면에 출력될 GameAnimation 객체가 추가되는 List
        List<GameAnimation> animations = new List<GameAnimation>();
        //animations에 들어있는 GameAnimation 객체의 Update를 위해 사용되는 List
        List<GameAnimation> removeAnimation = new List<GameAnimation>();

        ContentManager content;

        #endregion

        public ScreenManager()
        {

        }

        public void LoadContent(ContentManager Content)
        {
            content = Content;
        }

        #region Update & Draw
        
        public void Update(GameTime gameTime, Touch touch)
        {
            if (screenState == ScreenState.Active)
            {
                screensToUpdate.Clear();
                foreach (GameScreen screen in screens)
                    screensToUpdate.Add(screen);

                while (screensToUpdate.Count > 0)
                {
                    // 가장 위에 있는 것
                    GameScreen screen = screensToUpdate[screensToUpdate.Count - 1];
                    // 지움
                    screensToUpdate.RemoveAt(screensToUpdate.Count - 1);

                    // Update the screen.
                    screen.Update(touch, gameTime);
                }

                removeAnimation.Clear();
                foreach (GameAnimation animation in animations)
                    removeAnimation.Add(animation);

                while (removeAnimation.Count > 0)
                {
                    GameAnimation animation = removeAnimation[removeAnimation.Count - 1];

                    removeAnimation.RemoveAt(removeAnimation.Count - 1);

                    animation.Update(touch, gameTime);
                }
            }
            else if (screenState == ScreenState.Pause)
            {

            }
        }

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            foreach (GameScreen screen in screens)
            {
                screen.Draw(spriteBatch, gameTime);
            }

            foreach (GameAnimation animation in animations)
            {
                animation.Draw(spriteBatch);
            }
        }

        #endregion

        #region Helper Method

        public void AddScreen(GameScreen screen)
        {
            screen.ScreenManager = this;
            screens.Add(screen);
            screen.LoadContent(content);
        }
        public void RemoveScreen(GameScreen screen)
        {
            screens.Remove(screen);
        }

        public void AddAnimation(GameAnimation animation)
        {
            animation.ScreenManager = this;
            animations.Add(animation);
            animation.LoadContent(content);
        }
        public void RemoveAnimation(GameAnimation animation)
        {
            animations.Remove(animation);
        }
        #endregion
    }
}
